package com.yunji.order.command.retund.impl;

import com.yunji.common.event.SpringEventBus;
import com.yunji.common.event.entity.EventData;
import com.yunji.common.utils.TimeUtils;
import com.yunji.order.command.retund.AbsRefundCommand;
import com.yunji.order.domain.RefundDO;
import com.yunji.order.wechat.WechatCallbackDTO;
import com.yunji.order.dto.cmd.RefundFinishCommandDTO;
import com.yunji.order.event.OrderRefundScaleEvent;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author : peter-zhu
 * @date : 2025/3/28 11:24
 * @description : TODO
 **/
@Component
public class RefundFinishCommand extends AbsRefundCommand<RefundFinishCommandDTO, Boolean> {
    @Override
    public Boolean execute(RefundFinishCommandDTO cmd) {
        WechatCallbackDTO wechatCallback = cmd.getCallbackDTO();
        RefundDO refundDO = cmd.getRefundDO();
        RefundDO update = new RefundDO();
        update.setRefundId(refundDO.getRefundId());
        update.setFinishTime(new Date());
        update.setApplyStatus(cmd.getNewStatus().getCode());
        Date refundTime = TimeUtils.parseZoneDate(wechatCallback.getTime());
        String wechatCode = wechatCallback.getWechatCode();
        update.setWechatTime(refundTime);
        update.setWechatCode(wechatCode);
        if (refundDO.getWholeRefund() == 1)
            orderConsumer.backCoupon(refundDO.getOrderId());
        SpringEventBus.getInstance().publish(new EventData<>(new OrderRefundScaleEvent(refundDO, wechatCode, refundTime)));
        return refundService.updateRefund(update);
    }
}
